﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using xf.XfForm.Order;

namespace xf
{
    public partial class XFMainForm : Form
    {
        public XFMainForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 退出系统触发事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormClosingHandler(object sender, FormClosingEventArgs e)
        {
            DialogResult dialogResult = MessageBox.Show("您确定要退出系统吗？", "退出提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogResult == DialogResult.Yes)
            {
                Application.ExitThread();
            }
            else 
            {
                e.Cancel = false;
            }
        }

        /// <summary>
        /// 退出系统
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void subExitProgram_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        /// <summary>
        /// 开启“保存/修改客户信息”窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void subSaveCustomer_Click(object sender, EventArgs e)
        {
            if (CheckForm(typeof(FCustomerList).Name)) {
                FCustomerList.CreateInstance().Activate();
                return;
            }
            FCustomerList fSaveCustomer = FCustomerList.CreateInstance();
            ShowSubForm(fSaveCustomer);
        }

        /// <summary>
        /// 检查是否有开启对应form
        /// </summary>
        /// <param name="formName"></param>
        /// <returns></returns>
        private bool CheckForm(string formName) {
            bool result = false;
            foreach (Form f in Application.OpenForms) {
                if (f.Name == formName) {
                    result = true;
                    break;
                }
            }
            return result;
        }

        /// <summary>
        /// 开启“保存/修改产品信息”窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void subSaveGoods_Click(object sender, EventArgs e)
        {
            if (CheckForm(typeof(FGoodsList).Name))
            {
                FGoodsList.CreateInstance().Activate();
                return;
            }
            FGoodsList fGoodsList = FGoodsList.CreateInstance();
            ShowSubForm(fGoodsList);
        }

        /// <summary>
        /// 显示最大化的子窗口
        /// </summary>
        /// <param name="form"></param>
        private void ShowSubForm(Form form) 
        {
            form.WindowState = FormWindowState.Maximized;
            form.MdiParent = this;
            form.MaximizeBox = true;
            form.Show();
            form.Activate();
        }

        /// <summary>
        /// 开启“保存/修改订单”窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void subSaveOrder_Click(object sender, EventArgs e)
        {
            if (CheckForm(typeof(FOrderList).Name))
            {
                FOrderList.CreateInstance().Activate();
                return;
            }
            FOrderList fOrderList = FOrderList.CreateInstance();
            ShowSubForm(fOrderList);
        }
    }
}
